<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Url;
use think\Session;

class Lists extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function getPlayListByUid()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0]);
            die();
        }
        $uid=Session::get('uid');
        $result=db('playlist')->where('uid',$uid)->select();
        echo json_encode(['status'=>1,'length'=>count($result),'result'=>$result]);
    }

    public function getAllPlayList()
    {
        $result=db('playlist')->field('pl.pid,pl.pname,u.uid,u.nickname')->alias('pl')->join('users u','pl.uid=u.uid')->select();
        $playlistslide='';
        foreach ($result as $pl)
        {
            $playlistslide=$playlistslide. '<li class="list-group-item">
                                                <a href="javascript:;" onclick="play_PL(this)" data-pid="'.$pl['pid'].'" class="jp-play-me m-r-sm pull-left">
                                                    <i class="icon-control-play text"></i>
                                                    <i class="icon-control-play text-active"></i>
                                                </a>
                                                <div class="clear text-ellipsis">
                                                    <span><a href="javascript:;" data-pid="'.$pl['pid'].'" onclick="showPLDetail(this)">'.$pl['pname'].'</a></span>
                                                    <span class="text-muted"> -- <a href="javascript:;">'.$pl['nickname'].'</a></span>
                                                </div>
                                            </li>';
        }
        return $playlistslide;
    }


    public function addToPL($mid,$pid)
    {
        if(!Session::has('uid'))
        {
            json_encode(['status'=>2]);
            die();
        }
        try{
            if($pid==-1)
            {
                $Index=new Index();
                $lovelist=$Index->getLoveList();
                $flag=true;
                $key=array_search($mid,$lovelist);
                if($key!=false)
                {
                    echo json_encode(['status'=>1]);
                    die();
                }
                else
                {
                    $flag=true;
                    $lovelist[count($lovelist)]=$mid;
                }
                sort($lovelist);
                $lovelist=array_unique($lovelist);
                $sLovelist=implode(' ',$lovelist);
                Session::set('lovelist',$sLovelist);
                $aLovelist=['lovelist'=>$sLovelist];
                $result=\db('users')->where('uid',Session::get('uid'))->update($aLovelist);
            }
            else
            {
                $a=['mid'=>$mid,'pid'=>$pid];
                $result=db('plays')->insert($a);
            }
            echo json_encode(['status'=>1]);
            die();
        }
        catch (\mysqli_sql_exception $exception)
        {
            echo json_encode(['status'=>0,'info'=>$exception->getMessage()]);
            die();
        }
    }



    public function getPLDetail($pid)
    {
        $result=db('plays')->field('m.mid,m.file,m.mname,s.sname,m.poster,s.sid')->alias('pl')->join('musics m','pl.mid=m.mid')->join('singers s','s.sid=m.sid')->where('pl.pid',$pid)->select();
        echo json_encode($result);
        die();
    }


    public function getContent()
    {
        echo '<section class="hbox stretch bg-black dker">
                            <!-- side content -->
                            <aside class="col-sm-5 no-padder" id="sidebar">
                                <section class="vbox animated fadeInUp">
                                    <section class="scrollable">
                                        <div class="m-t-n-xxs item pos-rlt">
                                            <div class="top text-right">
                        <span class="musicbar animate bg-success bg-empty inline m-r-lg m-t" style="width:25px;height:30px">
                          <span class="bar1 a3 lter"></span>
                          <span class="bar2 a5 lt"></span>
                          <span class="bar3 a1 bg"></span>
                          <span class="bar4 a4 dk"></span>
                          <span class="bar5 a2 dker"></span>
                        </span>
                                            </div>
                                            <div class="bottom gd bg-info wrapper-lg">
                                            </div>
                                            <img class="img-full" src="'.Url::ImgUrl().'m43.jpg" alt="...">
                                        </div>
                                        <ul class="list-group list-group-lg no-radius no-border no-bg m-t-n-xxs m-b-none auto">
                                        '.$this->getAllPlayList().'
                                        </ul>
                                    </section>
                                </section>
                            </aside>
                            <!-- / side content -->
                            <section class="col-sm-4 no-padder bg">
                                <section class="vbox">
                                    <section class="scrollable hover">
                                        <ul id="PlayListSlide_Right" class="list-group list-group-lg no-bg auto m-b-none m-t-n-xxs">

                                        </ul>
                                    </section>
                                </section>
                            </section>
                        </section>';
        die();
    }
}
